// pages/bookshelf_enterprise/bookshelf_enterprise.js
const { getBookShelf } = require("../../network/bookshelf")
const { debounce } = require("../../utils/Tools")
const app = getApp()
Page({
  data: {
    change_more: false,
    before: [],
    list: [],
    thisPage: 1,
    type: '',
    key_work: '',
    fontSetSize: 'small', // 用户设置的字体大小（'small'：小，'middle'：中，'large'：大）
  },

  onChageMore(){
    if(this.data.change_more){
      this.setData({
        change_more:false
      })
    }else{
      this.setData({
        change_more:true
      })
    }
  },

  /**
   * 搜索框获取焦点
   */
  handleFocus () {
    this.setData({
      focus: true
    })
  },

  /**
   * 搜索框失去焦点
   */
  handleBlur (e) {
    let searchValue = e.detail.value
    if (!searchValue.length) {
      this.setData({
        focus: false
      })
    }
  },

  /**
   * 输入框输入内容
   */
  handleInput (e) {
    const that = this
    // 使用防抖提高性能
    if (that.data.timer) {
      clearTimeout(that.data.timer)
    }
    that.data.timer = setTimeout(() => {
      const key_work = e.detail.value
      that.setData({
        key_work,
        thisPage: 1,
        list: []
      }, () => {
        that.getBookShelf(that.data.type)
      })
    }, 500)
  },

  /**
   * 清空输入框内容
   */
  handleClearInput () {
    const that = this
    that.setData({
      inputValue: '',
      key_work: '',
      focus: false,
      thisPage: 1,
      list: []
    }, () => {
      that.getBookShelf(that.data.type)
    })
  },

  onLoad(options) {
    this.getBookShelf(options.type)
    this.setData({
      type: options.type
    })
    if (options.type == '1') {
      app.setTitle('企业书架')
    } else if (options.type == '2') {
      app.setTitle('盛和塾书架')
    }
    this.handleFilter = debounce(this.handleFilter)
  },

  // handleFilter(e){   ///  输入搜索框搜索内容
  //   const data = {
  //     type: this.data.type,
  //     key_work: e.detail
  //   }
  //   if(e.detail){
  //     getBookShelf(data).then(res=>{
  //       this.setData({
  //         list: res.data.data
  //       })
  //     }).catch(err=>{
  //       console.warn(err)
  //     })
  //   }
  // },

  // handleClearSearch(){
  //   this.setData({
  //     list: this.data.before
  //   })
  // },

  getBookShelf (id) {
    const that = this
    let thisPage = that.data.thisPage
    const data = {
      type: id,
      page: thisPage,
      key_work: that.data.key_work
    }
    getBookShelf(data).then(res=>{
      const list = that.data.list.length > 0 ? that.data.list.concat(res.data.data) : res.data.data
      that.setData({
        list
      })
      const lastPage = res.data.last_page
      if (lastPage > thisPage) {
        thisPage = thisPage + 1
        that.setData({
          thisPage
        }, () => {
          that.getBookShelf(that.data.type)
        })
      }
    }).catch(err=>{
      console.warn(err)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let fontSetSize = wx.getStorageSync('fontSetSize') // 本地缓存中用户设置的字体大小
    if (fontSetSize) {
      this.setData({
        fontSetSize
      })
    }
  },
})